# 1.拿到contID
# 2.拿到videoStatus返回的json -> srcURL
# 3.替换srcURL
# 4.下载视频

import requests

# 1.拿到拿到contID
url = "https://www.pearvideo.com/video_1795765"
# split用_切割,_前面是0,后面是1
contId = url.split("_")[1]
videoStatusUrl = f"https://www.pearvideo.com/videoStatus.jsp?contId={contId}&mrd=0.054683971269570364"

# 2.拿到videoStatus返回的json -> srcURL
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                  "AppleWebKit/537.36 (HTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
    # 防盗链：溯源，当前本次请求的上一级是谁
    "referer": url
}
resp = requests.get(videoStatusUrl, headers=headers)
# 字典接收返回的json数据
dic = resp.json()
srcUrl = dic["videoInfo"]["videos"]["srcUrl"]
systemTime = dic["systemTime"]

# 3.replace替换srcUrl
srcUrl = srcUrl.replace(systemTime, f"cont-{contId}")

# 4.下载视频
with open('li.mp4', mode='wb') as f:
    f.write(requests.get(srcUrl).content)
